﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Model;
using System.Threading;

namespace BLL
{
    public class KeyBLL
    {
        KeyDAL dal = new KeyDAL();
        KeysCategoryDAL keysCategoryDAL = new KeysCategoryDAL();
        

        public List<Key> GetAllKeys()
        {
            return dal.SelectAll();
        }

        public List<KeyView> GetAllKeyView()
        {
            List<KeyView> keyViewList = new List<KeyView>();
            var list = dal.GetAllKeyCategoryView();

            list.GroupBy(x => x.KeyItem.ID).ToList().ForEach(g =>
            {
                var list0 = list.Where(x => x.KeyItem.ID == g.Key).ToList();
                KeyView view = new KeyView { ID = list0[0].KeyItem.ID, Name = list0[0].KeyItem.Name };
                var categorys = from xx in list0 where xx.CategoryItem != null select xx.CategoryItem.ID;
                view.CategoryList = string.Join(",", categorys);
                keyViewList.Add(view);
            });
            return keyViewList;
        }

        public List<Key> GetAllKey()
        {
            List<Key> keyViewList = new List<Key>();
            var list = dal.SelectAll();
            return list;
        }

        public void Save(List<KeyView> list)
        {
            KeysCategory kc = null;
            Key key = null;
            list.ForEach(x =>
            {
                if (x.ID <= 0)//Insert
                {
                    key = new Key { Name = x.Name };
                    dal.Insert(key);
                    x.ID = key.ID;
                }
                else
                {
                    key = new Key { Name = x.Name, ID = x.ID };
                    dal.Update(key);
                    keysCategoryDAL.DeleteAllByKID(key.ID);
                }

                var categorys = x.CategoryList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string cid in categorys)
                {
                    kc = new KeysCategory { KID = key.ID, CID = int.Parse(cid) };
                    keysCategoryDAL.Insert(kc);
                }
            });
        }

        public void Delete(List<KeyView> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (!list[i].Selected) { continue; }
                if (list[i].ID > 0)
                {
                    dal.Delete(list[i]);
                    keysCategoryDAL.DeleteAllByKID(list[i].ID);
                }
            }
        }

        public void Insert(Key model)
        {
            dal.Insert(model);
        }

        public void Update(Key model)
        {
            dal.Update(model);
        }

        public void Delete(long p)
        {
            Key model = new Key { ID = p };
            Delete(model);
        }

        public void Delete(Key model)
        {
            dal.Delete(model);
        }

        public List<Key> SeachKeyByName(string name, List<long> filterID = null)
        {
            return dal.SeachKeyByName(name, filterID);
        }
    }
}
